package com.leyou.trade.web;

import com.leyou.trade.dto.PayResultDTO;
import com.leyou.trade.service.OrderService;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author BaiZY on 2020/9/10.
 * @version 1.0
 */
@RestController
@RequestMapping("/pay")
public class PayController {
    public final OrderService orderService;

    public PayController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 根据订单编号生成支付链接
     * @param orderId 订单编号
     * @return 支付链接
     */
    @GetMapping("/url/{id}")
    public ResponseEntity<String> getPayUrl(@PathVariable("id") Long orderId){
        return ResponseEntity.ok(orderService.getPayUrl(orderId));
    }

    /**
     * 处理微信的异步通知
     * @param data 通知内容
     * @return 处理结果
     */
    @PostMapping(value = "/wx/notify",produces = MediaType.APPLICATION_ATOM_XML_VALUE)
    public ResponseEntity<PayResultDTO> handleWxNotify(@RequestBody Map<String,String> data){
        orderService.handleWxNotify(data);
        return ResponseEntity.ok(new PayResultDTO());
    }

    /**
     * 主动查询订单
     * @param orderId
     * @return
     */
    @GetMapping("/status/{id}")
    public ResponseEntity<Integer> queryOrder(@PathVariable("id")Long orderId){
        return ResponseEntity.ok(orderService.activeQueryOrderStatus(orderId));
    }
}
